package com.fofia.iotweb.common;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.BitSet;

public class IoHelper {


    /**
     * byte转short*
     * @param buf 数组
     * @param asc 低字节在前
     * @return short data
     */
    public  static short getShort(byte[] buf, boolean asc) {
        short r = 0;

        if(asc)
        {

            for (int i =1; i >=0; i--) {
                r<<=8;
                r |= (buf[i] & 0xff);
            }

        }
        else
        {
            for (int i = 0; i < 2; i++) {
                r<<=8;
                r |= (buf[i] & 0xff);
            }

        }

        return r;
    }

    /**
     * byte[]转unint16->java中不支持unint16所以直接转为Int*
     * @param buf 数组
     * @param asc 低字节在前
     * @return short data
     */
    public  static int getunShort(byte[] data, boolean asc) {

        if(asc)
        {
            byte[] newBuff=arrayReverse(data);
            return Integer.valueOf(toHexString(newBuff),16);
        }

        return Integer.valueOf(toHexString(data),16);
    }

    /**
     *HEX字符串转为byte B2 -> 0xB2 *
     * @param in 输入的Hex字符串
     * @param  b 输出的byte数组
     * */
    public static int stringToByte(String in, byte[] b) throws Exception {
        if (b.length < in.length() / 2) {
            throw new Exception("byte array too small");
        }

        int j=0;
        StringBuffer buf = new StringBuffer(2);
        for (int i=0; i<in.length(); i++, j++) {
            buf.insert(0, in.charAt(i));
            buf.insert(1, in.charAt(i+1));
            int t = Integer.parseInt(buf.toString(),16);
            //System.out.println("byte hex value:" + t);
            b[j] = (byte)t;
            i++;
            buf.delete(0,2);
        }

        return j;
    }

    /**
     * HEX字符串转为byte[]数组*
     * @param in 输入HEX
     * @return the byte[]
     * */
    public static byte[] toHex(String in) throws Exception {
        byte[] b=new byte[in.length() / 2];

        int j=0;
        StringBuffer buf = new StringBuffer(2);
        for (int i=0; i<in.length(); i++, j++) {
            buf.insert(0, in.charAt(i));
            buf.insert(1, in.charAt(i+1));
            int t = Integer.parseInt(buf.toString(),16);
            //System.out.println("byte hex value:" + t);
            b[j] = (byte)t;
            i++;
            buf.delete(0,2);
        }

        return b;
    }

    /**
     * byte[] to Hex string.
     * @param byteArray the byte array
     * @return the string
     */
    public static String toHexString(byte[] byteArray) {
        final StringBuilder hexString = new StringBuilder();
        if (byteArray == null || byteArray.length <= 0)
        {
            return null;
        }
        for (int i = 0; i < byteArray.length; i++) {
            int v = byteArray[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                hexString.append(0);
            }
            hexString.append(hv);
        }
        return hexString.toString().toUpperCase();
    }

    /**
     *HEX字符串转字节数组 hex to byte[]
     * @param  hex hex字符串
     * @return 字节数组 byte[]
     * */
    public static byte[] toByteArray(String hex) {
        hex = hex.replace(" ","").toLowerCase();
        byte[] byteArray = new byte[hex.length() >> 1];
        int index = 0;
        for (int i = 0; i < hex.length(); i++) {
            if (index > hex.length() - 1)
            {
                return byteArray;
            }
            byte highDit = (byte)(Character.digit(hex.charAt(index), 16) & 0xFF);
            byte lowDit = (byte)(Character.digit(hex.charAt(index + 1), 16) & 0xFF);
            byteArray[i] = (byte)(highDit << 4 | lowDit);
            index += 2;
        }
        return byteArray;
    }

    /**
     * 数组翻转
     *
     * @param originArray the hex string
     * @return the byte [ ]
     */
    public static byte[] arrayReverse(byte[] originArray) {
        byte temp = 0;
        int length=originArray.length;
        for (int i = 0; i < length / 2; i++) {
            temp = originArray[i];
            originArray[i] = originArray[length - i - 1];
            originArray[length - i - 1] = temp;
        }
        return originArray;
    }
    /**
     *  byte to  int *
     * @param  buf 输入数组
     * @param  asc 低字节在前，
     * @param  len 数组长度
     *  */
    public static int getInt(byte[] buf, boolean asc, int len) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (len > 4) {
            throw new IllegalArgumentException("byte array size > 4 !");
        }

        int r = 0;
        if (asc)
        {
            for (int i = len - 1; i >= 0; i--) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        }
        else
        {
            for (int i = 0; i < len; i++) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        }
        return r;
    }

    /**
     *  整数转数组 int to byte[] *
     * @param  num 输入数值
     * @return  byte[]
     *  */
    public static byte[] intToBytes(int num) {
        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (num >>> (24 - i * 8));
        }

        return b;
    }

    /**
     *  整数转数组 short to byte[] *
     * @param  num 输入数值
     * @return  byte[]
     *  */
    public static byte[] shortToBytes(short num) {
        byte[] b = new byte[2];

        for (int i = 0; i < 2; i++) {
            b[i] = (byte) (num >>> (i * 8));
        }

        return b;
    }

    /**
     *  字节转HEX字符串byte to string *
     * @param  b 输入数值
     * @return  char
     *  */
    private static char findHex(byte b) {
        int t = new Byte(b).intValue();
        t = t < 0 ? t + 16 : t;

        if ((0 <= t) &&(t <= 9)) {
            return (char)(t + '0');
        }

        return (char)(t-10+'A');
    }

    /**
     *  字节转字符串 byte to  string*
     * @param  b 输入数值
     * @return  string hex 字符串
     *  */
    public static String byteToString(byte b) {
        byte high, low;
        byte maskHigh = (byte)0xf0;
        byte maskLow = 0x0f;

        high = (byte)((b & maskHigh) >> 4);
        low = (byte)(b & maskLow);

        StringBuffer buf = new StringBuffer();
        buf.append(findHex(high));
        buf.append(findHex(low));

        return buf.toString().toUpperCase();
    }

    /**
     * 整型转数组 short to byte[]*
     * @param s 输入数组
     * @param asc 小字节在前
     * @return 数组 byte[]
     *  */
    public  static byte[] getBytes(short s, boolean asc) {
        byte[] buf = new byte[2];
        if (asc){
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (s & 0x00ff);
                s >>= 8;
            }
        }
        else
        {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (s & 0x00ff);
                s >>= 8;
            }
        }
        return buf;
    }

    /**
     * 整型转数组 int to byte[]*
     * @param s 输入数组
     * @param asc 小字节在前
     * @return 数组 byte[]
     *  */
    public  static byte[] getBytes(int s, boolean asc) {
        byte[] buf = new byte[4];
        if (asc)
        {
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (s & 0x000000ff);
                s >>= 8;
            }
        }
        else
        {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (s & 0x000000ff);
                s >>= 8;
            }
        }
        return buf;
    }

    /**
     * 整型转数组 long to byte[]*
     * @param s 输入数组
     * @param asc 小字节在前
     * @return 数组 byte[]
     *  */
    public  static byte[] getBytes(long s, boolean asc) {
        byte[] buf = new byte[8];
        if (asc)
        {
            for (int i = buf.length - 1; i >= 0; i--) {
                buf[i] = (byte) (s & 0x00000000000000ff);
                s >>= 8;
            }
        }
        else
        {
            for (int i = 0; i < buf.length; i++) {
                buf[i] = (byte) (s & 0x00000000000000ff);
                s >>= 8;
            }
        }
        return buf;
    }

    /**
     * 数组转int byte[]->int*
     * @param buf 输入数组
     * @param asc 小字节在前
     * @return 整数 int
     *  */
    public  static int getInt(byte[] buf, boolean asc) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (buf.length > 4) {
            throw new IllegalArgumentException("byte array size > 4 !");
        }
        int r = 0;
        if (asc)
        {
            for (int i = buf.length - 1; i >= 0; i--) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        }
        else
        {
            for (int i = 0; i < buf.length; i++) {
                r <<= 8;
                r |= (buf[i] & 0x000000ff);
            }
        }
        return r;
    }

    /**
     * 数组转long byte[]->long*
     * @param buf 输入数组
     * @param asc 小字节在前
     * @return 整数 long
     *  */
    public  static long getLong(byte[] buf, boolean asc) {
        if (buf == null) {
            throw new IllegalArgumentException("byte array is null!");
        }
        if (buf.length > 8) {
            throw new IllegalArgumentException("byte array size > 8 !");
        }
        long r = 0;
        if (asc)
        {
            for (int i = buf.length - 1; i >= 0; i--) {
                r <<= 8;
                r |= (buf[i] & 0x00000000000000ff);
            }
        }
        else
        {
            for (int i = 0; i < buf.length; i++) {
                r <<= 8;
                r |= (buf[i] & 0x00000000000000ff);
            }
        }
        return r;
    }

    /**
     *
     * @author yushuo
     * @description bytes数组转bit,高字节在前
     * @date 12:11 2021/2/23
     * @param bytes
     * @return java.util.BitSet
     **/
    public static BitSet byteArray2BitSet(byte[] bytes) {
        int blen=bytes.length-1;
        BitSet bitSet = new BitSet(bytes.length * 8);
        int index = 0;
        for (int i = blen; i >=0; i--) {
            for (int j = 7; j >= 0; j--) {
                bitSet.set(index++, (bytes[i] & (1 << j)) >> j == 1? true: false);

            }
        }
        return bitSet;
    }

    /**
     *
     * @author yushuo
     * @description byte数组转bit
     * @date 12:11 2021/2/23
     * @param b
     * @return java.util.BitSet
     **/
    public static BitSet byte2Bit(byte b){
        BitSet bitSet = new BitSet(8);

        for (int j = 0; j < 8; j++) {
            //bitSet.set(index++, (b& (1 << j)) >> j == 1 ? true: false);
            bitSet.set(j,((b >> j) & 0x1)== 1 ? true: false);
        }
        return bitSet;
    }


    /**
     *
     * @author yushuo
     * @description bytes数组转bit,低字节在前
     * @date 12:11 2021/2/23
     * @param bytes
     * @return java.util.BitSet
     **/
    public static BitSet bytes2Bit(byte[] bytes){
        int blen=bytes.length-1;
        //int[] data=new int[8*bytes.length];
        int index=0;
        BitSet bitSet = new BitSet(bytes.length * 8);
//        for (int i =blen; i >=0; i--){
        for (int i =0; i<blen; i++){
            byte b=bytes[i];
            for (int j = 0; j < 8; j++) {
                bitSet.set(index++,((b >> j) & 0x1)== 1 ? true: false);
            }
//            data[index+1]=((b >>7) & 0x1);
//            data[index+1]=((b >> 6) & 0x1);
//            data[index+1]=((b >> 5) & 0x1);
//            data[index+1]=((b >> 4) & 0x1);
//            data[index+1]=((b >> 3) & 0x1);
//            data[index+1]=((b >> 2) & 0x1);
//            data[index+1]=((b >> 1) & 0x1);
//            data[index+1]=((b >> 0) & 0x1);
        }
//        return data;
        return bitSet;
    }

    public static double millsToGps(int mills)
    {
        double drgee= new BigDecimal((float)mills/3600000).setScale(6, BigDecimal.ROUND_HALF_UP).doubleValue();
        // (mills*100 / 3600000)/100.0;
        return  drgee;
    }
}

